﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Project
{
    public partial class EditForm : Form
    {
        public EditForm()
        {
            InitializeComponent();
        }
        public EditForm(int id, string title, string author, string countries)
        {
            InitializeComponent();

            this.id = id;
            txtTitle.Text = title;
            txtAuthor.Text = author;
            txtCountries.Text = countries;

        }

        private int id;
        /// <summary>
        /// 确认保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var title = txtTitle.Text;
            var author = txtAuthor.Text;
            var countires = txtCountries.Text;
            //修改
            if (id > 0)
            {
                if (!string.IsNullOrEmpty(title))
                 {
                    var sql = string.Format("update Articles set Title='{0}',Author='{1}',Countries='{2}'where Id={3}", title, author, countires, this.id);
                    var res = DbHelper.AddOrEditOrDeleteSave(sql);

                    if (res == 1)
                    {
                        MessageBox.Show("修改成功！", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
            }
            //新增
            else
            {
                if (!string.IsNullOrEmpty(title))
                {
                    var sql = string.Format("insert into Articles(Title,Author,Countries)values('{0}','{1}','{2}')", title, author, countires);

                    var res = DbHelper.AddOrEditOrDeleteSave(sql);

                    if (res == 1)
                    {
                        MessageBox.Show("添加成功！", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    //else
                    //{
                    //    MessageBox.Show("添加失败！", "提示");
                    //    this.Close();
                    //}
                }
                else
                {

                    MessageBox.Show("文章名不能为空！", "提示");
                }
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }
    }
}
